import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);

const routes = [{
        path: "/",
        name: "Home",
        component: Home,
    },
    {
        path: "/Page",
        name: "Page",
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Page.vue"),
    },
    {
        path: "/login",
        name: "Login",

        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Login.vue"),
    },
    {
        path: "/register",
        name: "Register",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Register.vue"),
    },
    // {


    //     path: '/*',
    //     name: '404',

    //     component: () => import(/* webpackChunkName: "404" */ '../views/404.vue')
    //   },

    {
        path: "/create",
        name: "Create",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Create.vue"),
    },
    {
        path: "/baberrage",
        name: "Baberrage",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Baberrage.vue"),
    },
    {
        path: "/percentage",
        name: "Percentage",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Percentage.vue"),
    },
    {
        path: "/test5",
        name: "Test5",
        children: [{
            path: "/test5/create",
            name: "Create",
            // route level code-splitting
            // this generates a separate chunk (about.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () =>
                import ( /* webpackChunkName: "about" */ "../views/Create.vue"),
        }, ],

        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Test5.vue"),
    },
    {
        path: "/personage",
        name: "Personage",
        children: [{
                path: "/personage/personage2",
                name: "Personage2",
                // route level code-splitting
                // this generates a separate chunk (about.[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () =>
                    import ( /* webpackChunkName: "about" */ "../views/Personage2.vue"),
            },
            {
                path: "/personage/weiguan",
                name: "Weiguan",
                // route level code-splitting
                // this generates a separate chunk (about.[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () =>
                    import ( /* webpackChunkName: "about" */ "../views/Weiguan.vue"),
            },
        ],
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Personage.vue"),
    },
    {
        path: "/index",
        name: "Index",
        children: [{
                path: "/index/danmu",
                name: "Danmu",
                // route level code-splitting
                // this generates a separate chunk (about.[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () =>
                    import ( /* webpackChunkName: "about" */ "../views/Danmu.vue"),
            },
            {
                path: "/index/kong",
                name: "Kong",

                component: () =>
                    import ( /* webpackChunkName: "about" */ "../views/Kong.vue"),
            },
        ],
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Index.vue"),
    }

];

const router = new VueRouter({
    routes,
});

//挂在路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === "/login") return next();
    const tokenStr = window.sessionStorage.getItem("token");
    if (to.path === "/register") next();
    else if (!tokenStr) return next("/login");
    else next();
});

export default router;